---
title: "Additional analysis"
output:
  html_document:
    df_print: paged
---

# Preparing the dataset

```{r, warning=FALSE, message=FALSE}
rm(list=ls(all=TRUE))
Sys.setenv(LANG = "en")
options(scipen=999)

library(tidyverse)
library(ggplot2)
library(ggrepel)
library(cowplot)
library(stringr)
library(ggsci)
library(forcats)

path <- "C:\\users\\borbath\\box.fu\\Papers_not_with_Swen\\Eastern_Europe_cs\\empirical_analysis\\syntax\\Harvard_dataverse\\"

ballanced <- read.csv(paste0(path, "ballanced_dat.csv"))

RO <- ballanced %>% 
  filter(iso2code=="RO")

pos_sal_cen <- ballanced %>%
  group_by(country_year, issue_cat) %>% 
  # filter(perc_party!=0 & !is.na(perc_party)) %>%
  mutate(nr_parties=sum(!is.na(perc_party))) %>% 
  mutate(possal=party_pos*party_sal,
    possal_cen=(possal)-((sum(possal, na.rm=TRUE)-(possal))/(nr_parties-1)), # leave self out mean
         weightedmean=sum(party_pos*party_sal*perc_party, na.rm = TRUE)/sum(perc_party, na.rm = TRUE)) %>% 
  select(iso2code, country, year_election, country_year,
         party_id, party_name_short, perc_party, type,
         issue_cat, issue_string, perc_issue, possal,
         possal_cen, count_issuebyparty, weightedmean)

# mergers, splits, alliances, everything for what cont. can be established

# parties with one child

org_continuity <- pos_sal_cen %>% 
  filter((country_year=="AU_2006" & party_id==973)|
           (country_year=="AU_2008" & party_id==955) |
           (country_year=="AU_2013" & party_id==1429) |
           (country_year=="FR_2007" & party_id==658) |
           (country_year=="FR_2012" & party_id==1539) |
           (country_year=="FR_2012" & party_id==686) |
           (country_year=="FR_2012" & party_id==401) |
           (country_year=="IE_2011" & party_id==318) |
           (country_year=="CH_2007" & party_id==750) |
           (country_year=="NL_2012" & party_id==742) |
           (country_year=="PT_2015" & party_id==1273) |
           (country_year=="ES_2004" & party_id==894) |
           (country_year=="ES_2008" & party_id==894) |
           (country_year=="ES_2011" & party_id==894) |
           (country_year=="ES_2015" & party_id==2376) |
           (country_year=="ES_2016" & party_id==2376) |
           (country_year=="GR_2015" &  party_id==1592) |
           (country_year=="GR_2015.5" & party_id==1592) |
           (country_year=="GR_2009" & party_id==47) |
           (country_year=="GR_2012" & party_id==1592) |
           (country_year=="GR_2012.5" & party_id==1338) |
           (country_year=="IT_2013" & party_id==1321) |
           (country_year=="IT_2008" & party_id==382) |
           (country_year=="IT_2013" & party_id==596) |
           (country_year=="LV_2006" & party_id==1656) |
           (country_year=="LV_2014" & party_id==2366) |
           (country_year=="LV_2010" & party_id==811) |
           (country_year=="LV_2014" & party_id==1666) |
           (country_year=="LV_2011" & party_id==662) |
           (country_year=="PL_2007" & party_id==528) |
           (country_year=="PL_2007" & party_id==512) |
           (country_year=="HU_2006" & party_id==95) |
           (country_year=="HU_2010" & party_id==1591) |
           (country_year=="RO_2012" & party_id==5) |
           (country_year=="RO_2004" & party_id==419) |
           (country_year=="RO_2012" & party_id==958)) %>% 
  select(country_year, party_id, issue_cat, possal_cen, count_issuebyparty, 
         possal, weightedmean) %>% 
  rename(matching_with=party_id) %>% 
  mutate(party_id=case_when((country_year=="AU_2006" & matching_with==973) ~ 955, # LiF ran w SPO
                            (country_year=="AU_2008" & matching_with==955) ~ 2255, # NEOS comes from a merger with LiF
                            (country_year=="AU_2013" & matching_with==1429) ~ 2651, # Liste PILZ from the Greens
                            (country_year=="FR_2007" & matching_with==658) ~ 2399, # DLR/DLF is a split from UMP
                            (country_year=="FR_2012" & matching_with==1539) ~ 2643, # Macron comes from PS
                            (country_year=="FR_2012" & matching_with==686) ~ 2644, # Melanchon runing in '12
                            (country_year=="FR_2012" & matching_with==401) ~ 2273, # UDI comes from PR
                            (country_year=="IE_2011" & matching_with==318) ~ 2619, # DS was heavily linked to Labor
                            (country_year=="CH_2007" & matching_with==750) ~ 1213, # BDP comes from SVP 
                            (country_year=="NL_2012" & matching_with==742) ~ 2641, # DENK comes from PvdA
                            (country_year=="PT_2015" & matching_with==1273) ~ 251, # CDS-PP ran together with PSD
                            (country_year=="ES_2004" & matching_with==894) ~ 2605, # CDC ran with CiU
                            (country_year=="ES_2008" & matching_with==894) ~ 2605, # CDC ran with CiU
                            (country_year=="ES_2011" & matching_with==894) ~ 2605, # CDC ran with CiU
                            (country_year=="ES_2015" & matching_with==2376) ~ 2606, # En comu and Podemos
                            (country_year=="ES_2016" & matching_with==2376) ~ 118, # PCE/IU ran with Podemos
                            (country_year=="GR_2015" & matching_with==1592) ~ 1280, # OP ran with Syriza
                            (country_year=="GR_2015.5" & matching_with==1592) ~ 1280, # OP ran with Syriza
                            (country_year=="GR_2009" & matching_with==47) ~ 2091, # ANEL as a splinter from ND
                            (country_year=="GR_2012" & matching_with==1592) ~ 2093, # Dimar as a splinter from Syriza
                            (country_year=="GR_2012.5" & matching_with==1338) ~ 2407, # Kidiso from Pasok
                            (country_year=="IT_2013" & matching_with==1321) ~ 693, #PRC and IdV were running together
                            (country_year=="IT_2008" & matching_with==382) ~ 1296, #Ri in coalition with PD
                            (country_year=="IT_2013" & matching_with==596) ~ 2154, #FdI split from PDL
                            (country_year=="LV_2006" & matching_with==1656) ~ 239, #KDS in coal. with LSDSP
                            (country_year=="LV_2014" & matching_with==2366) ~ 239, #KDS in coal. with LRA
                            (country_year=="LV_2010" & matching_with==811) ~ 662, #TP in coal. with LPP/LC
                            (country_year=="LV_2014" & matching_with==1666) ~ 1942, # RP in coal with Unity
                            (country_year=="LV_2011" & matching_with==662) ~ 2367, #ViLa is a succ. of LPP/LC
                            (country_year=="PL_2007" & matching_with==528) ~ 1679, #PjN is a split from PiS
                            (country_year=="PL_2007" & matching_with==512) ~ 1945, #Palikot was in PO
                            (country_year=="HU_2006" & matching_with==95) ~ 600, # Jobbik in coal with MIEP
                            (country_year=="HU_2010" & matching_with==1591) ~ 1970, # DK is a split from MSZP
                            (country_year=="RO_2012" & matching_with==5) ~ 2647, # ALDE from PC
                            (country_year=="RO_2004" & matching_with==419) ~ 958, # PDL from PD
                            (country_year=="RO_2012" & matching_with==958) ~ 2395, # PMP from PDL
  )) %>%
  select(-matching_with)

pos_sal_cen <- merge(pos_sal_cen, org_continuity, by=c("country_year", "party_id", "issue_cat"), all.x = TRUE)
pos_sal_cen <- pos_sal_cen[with(pos_sal_cen, order(country, year_election, party_id, issue_cat)),]

pos_sal_cen <- pos_sal_cen %>% 
  mutate(possal=ifelse(!is.na(possal.y), possal.y, possal.x),
         weightedmean=ifelse(!is.na(weightedmean.y), weightedmean.y, weightedmean.x),
         possal_cen=ifelse(!is.na(possal_cen.y), possal_cen.y, possal_cen.x),
         count_issuebyparty=ifelse(!is.na(count_issuebyparty.y), count_issuebyparty.y, count_issuebyparty.x)) %>% 
  select(-c(possal_cen.y, possal.y, weightedmean.y, count_issuebyparty.y,
            possal_cen.x, possal.x, weightedmean.x, count_issuebyparty.x))

rm(org_continuity)

# parties with two children

org_continuity <- pos_sal_cen %>% 
  filter((country_year=="FR_2007" & party_id==658) |
           (country_year=="GR_2009" & party_id==47) |
           (country_year=="GR_2015" & party_id==1592) |
           (country_year=="IT_2013" & party_id==1321) |
           (country_year=="HU_2010" & party_id==1591)) %>% 
  select(country_year, party_id, issue_cat, possal_cen, count_issuebyparty,
         possal, weightedmean) %>% 
  rename(matching_with=party_id) %>% 
  mutate(party_id=case_when((country_year=="FR_2007" & matching_with==658) ~ 401, # PR supp. UMP
                            (country_year=="GR_2009" & matching_with==47) ~ 2094, # Disy from ND
                            (country_year=="GR_2015" & matching_with==1592) ~ 2596, # LE from Syriza
                            (country_year=="IT_2013" & matching_with==1321) ~ 465,# PRC in SEL
                            (country_year=="HU_2010" & matching_with==1591) ~ 2318 # Egyutt from MSZP
  )) %>%
  select(-matching_with)

pos_sal_cen <- merge(pos_sal_cen, org_continuity, by=c("country_year", "party_id", "issue_cat"), all.x = TRUE)
pos_sal_cen <- pos_sal_cen[with(pos_sal_cen, order(country, year_election, party_id, issue_cat)),]

pos_sal_cen <- pos_sal_cen %>% 
  mutate(possal=ifelse(!is.na(possal.y), possal.y, possal.x),
         weightedmean=ifelse(!is.na(weightedmean.y), weightedmean.y, weightedmean.x),
         possal_cen=ifelse(!is.na(possal_cen.y), possal_cen.y, possal_cen.x),
         count_issuebyparty=ifelse(!is.na(count_issuebyparty.y), count_issuebyparty.y, count_issuebyparty.x)) %>% 
  select(-c(possal_cen.y, possal.y, weightedmean.y, count_issuebyparty.y,
            possal_cen.x, possal.x, weightedmean.x, count_issuebyparty.x))

rm(org_continuity)

all_comb <- pos_sal_cen %>% 
  select(iso2code, year_election, issue_cat, party_id) %>% 
  distinct(.)
```

# Programmatic volatility scores

```{r, warning=FALSE, message=FALSE}
prog_vol_B <- pos_sal_cen %>%
  group_by(iso2code, party_id, issue_cat) %>% 
  arrange(year_election, .by_group=TRUE) %>%
  mutate(progvolB_prty=abs(possal_cen - lag(possal_cen))*perc_party*perc_issue) %>% 
  filter(type=="Type B" | type=="Last elec bfr dying" | type=="First Election") %>%  #the missings in possal_cen are due to parties which are only in the first election and then they disappear (their last election is also the first in the sample).
  group_by(iso2code, year_election) %>%
  mutate(progvolB=sum(progvolB_prty, na.rm = TRUE)) %>%
  mutate(progvol_sys=progvolB,
         progvol_prty=progvolB_prty) %>% 
  select(-c(country_year, possal, possal_cen, weightedmean,
            perc_issue, perc_party, count_issuebyparty, country))

lagged_means <- pos_sal_cen %>% 
  
  select(iso2code, year_election, issue_cat, weightedmean) %>% 
  distinct(.) %>% 
  group_by(iso2code, issue_cat) %>% 
  arrange(year_election, .by_group=TRUE) %>%
  mutate(lag_weightedmean=lag(weightedmean)) %>% 
  select(-weightedmean) %>% 
  filter(!is.na(lag_weightedmean)) %>% 
  ungroup(.)

prog_vol_A_gn <- merge(pos_sal_cen, lagged_means, by=c("iso2code", "year_election", "issue_cat"), all.x = TRUE)

prog_vol_A_gn <- prog_vol_A_gn %>% 
  mutate(progvolA_new_prty=abs(possal - lag_weightedmean)*perc_party*perc_issue) %>%
  filter(type=="Type A (new)" | type=="Type A (new) & Last elec bfr dying") %>%
  group_by(iso2code, year_election) %>%
  mutate(progvolA_new=sum(progvolA_new_prty, na.rm = TRUE)) %>% 
  select(-lag_weightedmean)

lead_means <- pos_sal_cen %>% 
  select(iso2code, year_election, issue_cat, weightedmean) %>% 
  distinct(.) %>% 
  group_by(iso2code, issue_cat) %>% 
  arrange(year_election, .by_group=TRUE) %>%
  mutate(lead_weightedmean=lead(weightedmean)) %>% 
  select(-weightedmean) %>% 
  filter(!is.na(lead_weightedmean)) %>% 
  ungroup(.)

prog_vol_A_old <- merge(pos_sal_cen, lead_means, by=c("iso2code", "year_election", "issue_cat"), all.x = TRUE)

prog_vol_A_old <- prog_vol_A_old %>% 
  mutate(delta_possal=abs(possal - lead_weightedmean)*perc_party*perc_issue) %>%
  filter(type=="Last elec bfr dying" | type=="Type A (new) & Last elec bfr dying") %>%
  group_by(iso2code, year_election) %>%
  mutate(progvolA_old=sum(delta_possal, na.rm = TRUE)) %>% 
  ungroup(.) %>% 
  select(-lead_weightedmean)

all_cntry_elec <- pos_sal_cen %>% 
  select(iso2code, year_election, country_year, issue_cat) %>% 
  distinct(.) %>%
  group_by(iso2code, issue_cat) %>% 
  arrange(year_election, .by_group=TRUE) %>% 
  mutate(lagyear=lag(year_election))

prog_vol_A_old <- merge(prog_vol_A_old, all_cntry_elec, all.x = TRUE)

prog_vol_A_old <- prog_vol_A_old %>% 
  select(-country_year, - year_election) %>% 
  filter(!is.na(lagyear)) %>% 
  rename(progvolA_old_prty = delta_possal,
         year_election = lagyear)

prog_vol_A <- merge(prog_vol_A_gn, prog_vol_A_old, all=TRUE)

prog_vol_A <- prog_vol_A %>%
  mutate_at(vars(progvolA_new_prty, progvolA_new,
                        progvolA_old_prty, progvolA_old), ~replace_na(., 0)) %>%
  mutate(progvol_sys=progvolA_new+progvolA_old,
         progvol_prty=progvolA_new_prty+progvolA_old_prty) %>% 
  select(-c(country_year, possal_cen, weightedmean,
            perc_issue, perc_party, count_issuebyparty,
            possal, country))

rm(lagged_means, prog_vol_A_gn, lead_means, pos_sal_cen, all_cntry_elec, prog_vol_A_old)

prog_vol <- merge(all_comb, prog_vol_B, all=TRUE)
prog_vol <- merge(prog_vol, prog_vol_A, all=TRUE)
rm(prog_vol_A, prog_vol_B, all_comb)


first_elect <- ballanced %>% 
  select(iso2code, year_election) %>% 
  distinct(.) %>% 
  group_by(iso2code) %>% 
  arrange(year_election, .by_group=TRUE) %>% 
  mutate(year_election = replace(year_election, row_number() == 1, NA)) %>% 
  filter(!is.na(year_election)) %>% 
  ungroup(.)

prog_vol <- merge(prog_vol, first_elect) 
rm(first_elect)

prog_vol <- prog_vol %>%
  filter(!(is.na(type) | is.na(progvol_prty))) %>% 
  mutate(country_year=ifelse(year_election%%1==0,
                             paste0(iso2code, "_", str_sub(year_election, start= -2)),
                                    paste0(iso2code, "_", str_sub(year_election, start= -4))),
         region = case_when(
    iso2code %in% c("AU", "CH", "DE", "FR", "IE", "NL", "UK") ~ 1,
    iso2code %in% c("ES", "GR", "IT", "PT") ~ 2,
    iso2code %in% c("HU", "LV", "PL", "RO") ~ 3))

```

#Programmatic volatility

```{r, warning=FALSE, message=FALSE}

# is_outlier <- function(x) {
#   return(x < quantile(x, 0.25) - 1.5 * IQR(x) | x > quantile(x, 0.75) + 1.5 * IQR(x))
# }

is_outlier <- function(x) {
  return(x > quantile(x, 0.85) + 1.5 * IQR(x))
}


ch_issue <- prog_vol %>% 
  group_by(country_year, issue_string) %>% 
  mutate(ch_issue=sum(progvol_prty)) %>% 
  select(country_year, issue_string, ch_issue, region) %>% 
  distinct(.) %>% 
  group_by(issue_string) %>% 
  mutate(outlier = ifelse(is_outlier(ch_issue), country_year, NA))


ggplot(ch_issue, aes(y=ch_issue, x=as.factor(issue_string))) +
  geom_boxplot() +
  geom_text_repel(aes(label = outlier), size=2, segment.color=NA, 
                  point.padding = unit(1, "points"), nudge_y = 1.0E-6, na.rm = TRUE) +
  theme_bw() +
  ylab("Total programmatic change") +
  scale_x_discrete(limits = rev(levels(ch_issue$issue_string))) +
  theme(plot.title = element_text(hjust = 0.5),
        legend.position="none",
        axis.title.y=element_blank()) + coord_flip()

ggsave(paste0(path, "\\appendix\\boxplot_cntr_iss.png"), plot=last_plot(),
          width=5, height=6)

  
```

```{r, warning=FALSE, message=FALSE}

ch_issue <- prog_vol %>% 
  group_by(iso2code, issue_string, year_election) %>% 
  mutate(ch_issue=sum(progvol_prty)) %>% 
  select(iso2code, year_election, issue_string, ch_issue, region) %>% 
  distinct(.) %>% 
  group_by(iso2code, issue_string) %>%
  mutate(ch_issue=sum(ch_issue)) %>%
  select(-year_election) %>% 
  distinct(.)

ggplot(ch_issue, aes(y=ch_issue, x=as.factor(iso2code))) +
  geom_bar(stat="identity") +
  theme_bw() +
  ylab("Total programmatic change") +
  scale_x_discrete(limits = rev(levels(ch_issue$iso2code))) +
  facet_wrap(~issue_string, nrow=4) +
  theme(plot.title = element_text(hjust = 0.5),
        legend.position="none",
        axis.title.y=element_blank()) + coord_flip()

ggsave(paste0(path, "\\appendix\\barplot_iss_cntr.png"), plot=last_plot(),
          width=6, height=7, scale=1.3)


ggplot(ch_issue, aes(y=ch_issue, x=as.factor(issue_string))) +
  geom_bar(stat="identity") +
  theme_bw() +
  ylab("Total programmatic change") +
  scale_x_discrete(limits = rev(levels(ch_issue$issue_string))) +
  facet_wrap(~iso2code, nrow=4) +
  theme(plot.title = element_text(hjust = 0.5),
        legend.position="none",
        axis.title.y=element_blank()) + coord_flip()

ggsave(paste0(path, "\\appendix\\barplot_cntr_iss.png"), plot=last_plot(),
          width=6, height=7, scale=1.3)

```

```{r, warning=FALSE, message=FALSE}
ch_issue <- prog_vol %>% 
  group_by(iso2code, issue_string, year_election) %>% 
  mutate(ch_issue=sum(progvol_prty)) %>% 
  select(iso2code, year_election, issue_string, ch_issue, region) %>% 
  distinct(.) %>% 
  group_by(iso2code, issue_string) %>%
  arrange(year_election, .by_group=TRUE) %>% 
  mutate(counter=as.character(seq_along(year_election))) %>% 
  mutate(count_fac = plyr::revalue(counter, c("1"="Second", "2"="Third", "3"="Fourth", "4"="Fifth", "5"="Sixth")))

ch_issue$count_fac <- as.factor(ch_issue$count_fac)
ch_issue$count_fac <- factor(ch_issue$count_fac, levels = c("Second", "Third", "Fourth", "Fifth", "Sixth"))

table(ch_issue$counter, ch_issue$count_fac)

ggplot(ch_issue, aes(y=ch_issue, x=as.factor(issue_string))) +
  geom_bar(aes(fill=fct_rev(count_fac)), stat="identity") +
  theme_bw() +
  scale_fill_npg(breaks = c("Second", "Third", "Fourth", "Fifth", "Sixth"),
                 limits = c("Second", "Third", "Fourth", "Fifth", "Sixth"),
                 name = "Election") +
  ylab("Total programmatic change") +
  scale_x_discrete(limits = rev(levels(ch_issue$issue_string))) +
  facet_wrap(~iso2code, nrow=4) +
  theme(plot.title = element_text(hjust = 0.5),
        legend.position = c(0.87, 0.12),
        axis.title.y=element_blank()) + coord_flip()

ggsave(paste0(path, "\\appendix\\barplot_cntr_iss2.png"), plot=last_plot(),
          width=6, height=7, scale=1.3)

```

```{r, warning=FALSE, message=FALSE}

ch_issue <- prog_vol %>% 
  filter(iso2code == "IE") %>% 
  group_by(iso2code, year_election, party_id, issue_string) %>% 
  mutate(prty_ch=sum(progvol_prty)) %>% 
  select(iso2code, party_id, party_name_short, prty_ch, year_election, issue_string) %>% 
  distinct(.) %>% 
  mutate(party_name_short=as.factor(droplevels(party_name_short))) %>% 
  ungroup(.)
  
ggplot(ch_issue, aes(y=prty_ch, x=as.factor(party_name_short), fill = issue_string)) +
  geom_bar(stat="identity") +
  theme_bw() +
  ylab("Total programmatic change") +
  scale_x_discrete(limits = rev(levels(ch_issue$party_name_short))) +
  scale_fill_d3("category20") +
  facet_wrap(~year_election) +
  theme(legend.title = element_blank(),
        plot.title = element_text(hjust = 0.5),
        axis.title.y=element_blank()) + coord_flip()

ggsave(paste0(path, "\\appendix\\ie_elec_iss.png"), plot=last_plot(),
          width=5, height=3, scale=1.8)
```
```{r, warning=FALSE, message=FALSE}
ch_issue <- prog_vol %>% 
  filter(iso2code == "RO") %>% 
  group_by(iso2code, year_election, party_id, issue_string) %>% 
  mutate(prty_ch=sum(progvol_prty)) %>% 
  select(iso2code, party_id, party_name_short, prty_ch, year_election, issue_string) %>% 
  distinct(.) %>% 
  mutate(party_name_short=as.factor(droplevels(party_name_short))) %>% 
  ungroup(.)

subset <- ch_issue %>% 
  filter(issue_string=="welfare") %>% 
  mutate(issue_string="immig") %>% 
  mutate(prty_ch=0)

ch_issue <- rbind(ch_issue, subset) # to have the same color scheme, it does not affect the plot otherwise
  
ggplot(ch_issue, aes(y=prty_ch, x=as.factor(party_name_short), fill = issue_string)) +
  geom_bar(stat="identity") +
  theme_bw() +
  ylab("Total programmatic change") +
  scale_x_discrete(limits = rev(levels(ch_issue$party_name_short))) +
  scale_fill_d3("category20") +
  facet_wrap(~year_election) +
  theme(legend.title = element_blank(),
        plot.title = element_text(hjust = 0.5),
        axis.title.y=element_blank()) + coord_flip()

ggsave(paste0(path, "\\appendix\\ro_elec_iss.png"), plot=last_plot(),
          width=5, height=3, scale=1.8)

rm(subset)
```

```{r, warning=FALSE, message=FALSE}

ch_issue <- prog_vol %>% 
  filter(iso2code == "LV") %>% 
  group_by(iso2code, year_election, party_id, issue_string) %>% 
  mutate(prty_ch=sum(progvol_prty)) %>% 
  select(iso2code, party_id, party_name_short, prty_ch, year_election, issue_string) %>% 
  distinct(.) %>% 
  mutate(party_name_short=as.factor(droplevels(party_name_short))) %>% 
  ungroup(.)
  
ggplot(ch_issue, aes(y=prty_ch, x=as.factor(party_name_short), fill = issue_string)) +
  geom_bar(stat="identity") +
  theme_bw() +
  ylab("Total programmatic change") +
  scale_x_discrete(limits = rev(levels(ch_issue$party_name_short))) +
  scale_fill_d3("category20") +
  facet_wrap(~year_election) +
  theme(legend.title = element_blank(),
        plot.title = element_text(hjust = 0.5),
        axis.title.y=element_blank()) + coord_flip()

ggsave(paste0(path, "\\appendix\\lv_elec_iss.png"), plot=last_plot(),
          width=5, height=3, scale=1.8)
```
```{r, warning=FALSE, message=FALSE}

ch_issue <- prog_vol %>% 
  filter(iso2code == "UK") %>% 
  group_by(iso2code, year_election, party_id, issue_string) %>% 
  mutate(prty_ch=sum(progvol_prty)) %>% 
  select(iso2code, party_id, party_name_short, prty_ch, year_election, issue_string) %>% 
  distinct(.) %>% 
  mutate(party_name_short=as.factor(droplevels(party_name_short))) %>% 
  ungroup(.)
  
ggplot(ch_issue, aes(y=prty_ch, x=as.factor(party_name_short), fill = issue_string)) +
  geom_bar(stat="identity") +
  theme_bw() +
  ylab("Total programmatic change") +
  scale_x_discrete(limits = rev(levels(ch_issue$party_name_short))) +
  scale_fill_d3("category20") +
  facet_wrap(~year_election) +
  theme(legend.title = element_blank(),
        plot.title = element_text(hjust = 0.5),
        axis.title.y=element_blank()) + coord_flip()

ggsave(paste0(path, "\\appendix\\uk_elec_iss.png"), plot=last_plot(),
          width=5, height=3, scale=1.8)
```
